package com.jsgt.greekfire.dao.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.Observer
import com.jsgt.greekfire.dao.AppDatabase
import com.jsgt.greekfire.dao.dao.IdentityBeanDao
import com.jsgt.greekfire.dao.dao.WalletBeanDao
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.model.WalletBean

class WalletViewModel(application: Application) : AndroidViewModel(application) {
    private var identityBeanDao: IdentityBeanDao? = null
    private var walletBeanDao: WalletBeanDao? = null

    init {
        identityBeanDao = AppDatabase.getInstance(application).identityBeanDao()
        walletBeanDao = AppDatabase.getInstance(application).walletBeanDao()
    }

    fun getAllIdentity(): LiveData<List<IdentityBean>>? {
        return identityBeanDao?.get()
    }

    fun getIdentity(id: Int): LiveData<IdentityBean>? {
        return identityBeanDao?.get(id)
    }

    fun addIdentity(identityBean: IdentityBean) {
        identityBeanDao?.add(identityBean)
    }

    fun deleteIdentity(identityBean: IdentityBean?) {
        identityBeanDao?.delete(identityBean)
    }

    fun deleteIdentity(id: Int) {
        identityBeanDao?.delete(id)
    }

    fun clearIdentity() {
        identityBeanDao?.clear()
    }

    fun updateIdentityBackup(identityBackup: IdentityBean.Backup) {
        identityBeanDao?.updateBackup(identityBackup)
    }

    fun updateIdentityName(identityName: IdentityBean.Name) {
        identityBeanDao?.updateName(identityName)
    }

    fun getAllWallet(): LiveData<List<WalletBean>>? {
        return walletBeanDao?.get()
    }

    fun getWallet(id: Int): LiveData<WalletBean>? {
        return walletBeanDao?.get(id)
    }

    fun getWalletBySelection(selection: Int): LiveData<List<WalletBean>>? {
        return walletBeanDao?.getBySelection(selection)
    }

    fun addWallet(walletBean: WalletBean) {
        walletBeanDao?.add(walletBean)
    }

    fun deleteWallet(walletBean: WalletBean?) {
        walletBeanDao?.delete(walletBean)
    }

    fun updateWallet(walletBean: WalletBean) {
        walletBeanDao?.update(walletBean)
    }

    fun updateWalletName(name: WalletBean.Name) {
        walletBeanDao?.updateName(name)
    }

    fun cleanSelection() {
        walletBeanDao?.cleanSelection()
    }

    fun updateSelection(selection: Int, id: Int) {
        walletBeanDao?.updateSelection(selection, id)
    }

    fun getAllIdentityWallet(): LiveData<List<IdentityWallet?>>? {
        return walletBeanDao?.getIdentityWallet()
    }

    fun getIdentityWalletById(id: Int): LiveData<IdentityWallet>? {
        return walletBeanDao?.getIdentityWallet(id)
    }

    fun getSelectWallet(): LiveData<List<IdentityWallet>>? {
        return walletBeanDao?.getIdentityWalletBySelection(1)
    }
}